#include <unistd.h>
#include "Thread.hpp"
#include "LockGuard.hpp"

int tickets = 1000;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void threadRoutine(void *args){
    string message = static_cast<const char*>(args);
    while(true){{   
            LockGuard lockguard(&mutex);
            if(tickets > 0){
                usleep(1000);
                cout << message << " get a ticket: " << tickets-- << endl;
            }
            else{
                break;
            }
        }
        usleep(1000);
    }
}

int main(){
    Thread t1(1, threadRoutine, (void*)"linux1");
    Thread t2(1, threadRoutine, (void*)"linux2");
    Thread t3(1, threadRoutine, (void*)"linux3");

    t1.run();
    t2.run();
    t3.run();

    t1.join();
    t2.join();
    t3.join();

    return 0;
}